package nz.ac.massey.cs.se400_2013.ass3.gui;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import nz.ac.massey.cs.se400_2013.ass3.gui.SideTree;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Tool extends JPanel
                         implements ActionListener {
    /**
	 * @author jmcurtis
	 */
	private static final long serialVersionUID = 5384140478634699678L;
	static final private String NCOURSE = "course";
    static final private String NPAPER = "paper";
    static final private String NSTUDENT = "student";
    static final private String SAVE = "save";
    static final private String DELETE = "delete";
    static final private String EXIT = "exit";
    static private JButton save;
    static private JButton delete;
    static private JButton nPaper;
    static private JButton nStudent;

    public Tool() {
        super(new BorderLayout());

        //Create the toolbar.
        JToolBar toolBar = new JToolBar("Tool Bar");
        toolBar.setFloatable(false);
        addButtons(toolBar);

        //Lay out the main panel.
        setPreferredSize(new Dimension(450, 130));
        add(toolBar, BorderLayout.PAGE_START);
    }
    
    public static void toggleButtons(String string){
    	if (string != null && string != "Root"){delete.setEnabled(true);}
    	else{delete.setEnabled(false);}
    	if (string == "Course"){
    		nPaper.setEnabled(true);
    		nStudent.setEnabled(false);
    	} else if (string == "Paper"){
    		nPaper.setEnabled(false);
    		nStudent.setEnabled(true);
    	} else {
    		nPaper.setEnabled(false);
    		nStudent.setEnabled(false);
    	}
    }

    protected void addButtons(JToolBar toolBar) {
        JButton button = null;
        //new course button
        button = makeNavigationButton(NCOURSE, "Create a new course.", "New Course");
        toolBar.add(button);
        //new paper button
        nPaper = makeNavigationButton(NPAPER, "Create a new paper.", "New Paper");
        nPaper.setEnabled(false);
        toolBar.add(nPaper);
        //new student button
        nStudent = makeNavigationButton(NSTUDENT, "Create a new student.", "New Student");
        nStudent.setEnabled(false);
        toolBar.add(nStudent);
        //save button
        save = makeNavigationButton(SAVE, "Save the current item.", "Save");
        save.setEnabled(false);
        toolBar.add(save);
        //delete button
        delete = makeNavigationButton(DELETE, "Delete the current item.", "Delete");
        delete.setEnabled(false);
        toolBar.add(delete);
        //exit button
        button = makeNavigationButton(EXIT, "Exit the program.", "Exit");
        toolBar.add(button);
    }

    protected JButton makeNavigationButton(String actionCommand, String toolTip, String bText) {
        //Build each button.
        JButton button = new JButton();
        button.setActionCommand(actionCommand);
        button.setToolTipText(toolTip);
        button.addActionListener(this);
        button.setText(bText);
        return button;
    }

    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        // Handle each button.
        if (NCOURSE.equals(cmd)) {nCourse();}
        else if (NPAPER.equals(cmd)) {nPaper();}
        else if (NSTUDENT.equals(cmd)) {nStudent();}
        else if (SAVE.equals(cmd)) {save();}
        else if (DELETE.equals(cmd)) {delete();}
        else if (EXIT.equals(cmd)) {exit();}
    }
    
    public void nCourse(){
    	// TODO put new course generator here
    }
    public void nPaper(){
    	// TODO put new paper generator here
    }
    public void nStudent(){
    	// TODO put new student generator here
    }
    public void save(){
    	// TODO put save operation here
    }
    public void delete(){
    	// TODO put delete operation here
    }
    public void exit(){System.exit(0);}
    
    

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                SideTree sideTree = new SideTree();
                Tool tool = new Tool();
                //sideTree.addMessageListener(new SideTreeMessageListener());
                
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
                frame.setLayout(new GridLayout(2, 2));
                
                //Add content to the window.
                frame.add(tool);
                frame.add(sideTree);
           
                //Display the window.
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}